/*
 * MarcoDiametros.java
 *
 * Created on 7 de octubre de 2007, 13:18
 */

package es.atareao.ferraplan.gui;

//
//********************************IMPORTACIONES*********************************
//
import es.atareao.alejandria.gui.ErrorDialog;
import es.atareao.queensboro.db.Conector;
import es.atareao.ferraplan.lib.Fabricante;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author  Propietario
 */
public class DialogFabricantes extends javax.swing.JFrame {
    //
    //********************************CONSTANTES********************************
    //
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    //
    public final static int OP_ADD=0;
    public final static int OP_EDIT=1;
    public final static int OP_DELETE=2;
    public final static int OP_VIEW=3;
    //
    //******************************CONSTRUCTORES*******************************
    //
    
    /** Creates new form MarcoDiametros */
    public DialogFabricantes(Conector conector) {
        initComponents();
        this.setSize(410,430);
        this.setLocationRelativeTo(null);
        try {
            this.setFabricante(new Fabricante(conector));
        } catch (SQLException ex) {
            ErrorDialog.manejaError(ex,false);
        }
        this.refresca();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemAñadir = new javax.swing.JMenuItem();
        jMenuItemEditar = new javax.swing.JMenuItem();
        jMenuItemConsultar = new javax.swing.JMenuItem();
        jMenuItemBorrar = new javax.swing.JMenuItem();
        jAceptar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jMenuItemAñadir.setText("Añadir");
        jMenuItemAñadir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAñadirActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItemAñadir);

        jMenuItemEditar.setText("Editar");
        jMenuItemEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemEditarActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItemEditar);

        jMenuItemConsultar.setText("Consultar");
        jMenuItemConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemConsultarActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItemConsultar);

        jMenuItemBorrar.setText("Borrar");
        jMenuItemBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemBorrarActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItemBorrar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Fabricantes");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_exit.png"))); // NOI18N
        jAceptar.setToolTipText("Salir");
        jAceptar.setBorderPainted(false);
        jAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(jAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 340, 50, 50));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 310, 300));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_add.png"))); // NOI18N
        jButton1.setToolTipText("Añadir");
        jButton1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButton1.setBorderPainted(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 10, -1, -1));

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_edit.png"))); // NOI18N
        jButton2.setToolTipText("Editar");
        jButton2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButton2.setBorderPainted(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 60, -1, -1));

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_new.png"))); // NOI18N
        jButton3.setToolTipText("Consultar");
        jButton3.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButton3.setBorderPainted(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 110, -1, -1));

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_delete.png"))); // NOI18N
        jButton4.setToolTipText("Borrar");
        jButton4.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButton4.setBorderPainted(false);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 160, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, 390, 330));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItemBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemBorrarActionPerformed
        this.operaciones(OP_DELETE);
    }//GEN-LAST:event_jMenuItemBorrarActionPerformed

    private void jMenuItemConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemConsultarActionPerformed
        this.operaciones(OP_VIEW);
    }//GEN-LAST:event_jMenuItemConsultarActionPerformed

    private void jMenuItemEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemEditarActionPerformed
        this.operaciones(OP_EDIT);
    }//GEN-LAST:event_jMenuItemEditarActionPerformed

    private void jMenuItemAñadirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAñadirActionPerformed
        this.operaciones(OP_ADD);
    }//GEN-LAST:event_jMenuItemAñadirActionPerformed

    private void jAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAceptarActionPerformed
// TODO add your handling code here:
        doClose(RET_OK);
    }//GEN-LAST:event_jAceptarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.operaciones(OP_ADD);
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.operaciones(OP_EDIT);
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.operaciones(OP_VIEW);
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.operaciones(OP_DELETE);
}//GEN-LAST:event_jButton4ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAceptar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JMenuItem jMenuItemAñadir;
    private javax.swing.JMenuItem jMenuItemBorrar;
    private javax.swing.JMenuItem jMenuItemConsultar;
    private javax.swing.JMenuItem jMenuItemEditar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
    //
    // *********************************CAMPOS*********************************
    //
    private int _returnStatus = RET_CANCEL;
    private int _operacion;
    private Fabricante _fabricante;
    //
    //********************************METODOS***********************************
    //
    
    //
    //**************************METODOS AUXILIARES******************************
    //
    private void operaciones(int operacion){
        DialogFabricante md;
        String selectedId="";
        int selectedRow=this.jTable1.getSelectedRow();
        if(selectedRow>-1){
            selectedId=(String)this.jTable1.getValueAt(selectedRow,0);
        }
        try{
            switch(operacion){
                case OP_ADD:
                    md=new DialogFabricante(this,operacion,this.getFabricante());
                    md.setVisible(true);
                    if(md.getReturnStatus()==DialogDiametro.RET_OK){
                        if(md.getFabricante().insert()){
                            if(((String)this.jTable1.getValueAt(0,0)).equals("")){
                                this.jTable1.setValueAt(md.getFabricante().getValue("orden"),0,0);
                                this.jTable1.setValueAt(md.getFabricante().getLastInsertId(),0,1);
                                this.jTable1.setValueAt(md.getFabricante(),0,2);
                                this.jTable1.setValueAt(md.getFabricante().getValue("numero"),0,3);
                            }else{
                                //Diametro diametro=md.getDiametro().getLastInserted();
                                Fabricante fabricante=md.getFabricante();
                                Object[] fila={fabricante.getValue("orden"),fabricante.getId(),fabricante,fabricante.getValue("numero")};
                                ((DefaultTableModel)this.jTable1.getModel()).addRow(fila);
                            }
                        }
                    }
                    break;
                case OP_EDIT:
                    if(selectedRow>-1){
                        Fabricante fabricante=(Fabricante)this.jTable1.getValueAt(selectedRow,2);
                        md=new DialogFabricante(this,operacion,fabricante);
                        md.setVisible(true);
                        if(md.getReturnStatus()==DialogDiametro.RET_OK){
                            if(md.getFabricante().update()){
                                ((DefaultTableModel)this.jTable1.getModel()).setValueAt(md.getFabricante().getValue("orden"),selectedRow,0);
                                ((DefaultTableModel)this.jTable1.getModel()).setValueAt(selectedId,selectedRow,1);
                                ((DefaultTableModel)this.jTable1.getModel()).setValueAt(md.getFabricante(),selectedRow,2);
                                ((DefaultTableModel)this.jTable1.getModel()).setValueAt(md.getFabricante().getValue("numero"),selectedRow,3);
                            }
                        }
                    }
                    break;
                case OP_VIEW:
                    if(selectedRow>-1){
                        Fabricante fabricante=(Fabricante)this.jTable1.getValueAt(selectedRow,2);
                        md=new DialogFabricante(this,operacion,fabricante);
                        md.setVisible(true);
                        break;
                    }
                case OP_DELETE:
                    if(selectedRow>-1){
                        this.getFabricante().setId(selectedId);
                        if(this.getFabricante().delete()){
                            if(selectedRow==0){
                                this.jTable1.setValueAt("",0,0);
                                this.jTable1.setValueAt("",0,1);
                                this.jTable1.setValueAt("",0,2);
                                this.jTable1.setValueAt("",0,3);
                            }else{
                                ((DefaultTableModel)this.jTable1.getModel()).removeRow(selectedRow);
                            }
                        }
                    }
                    break;
            }
        }catch(SQLException ex){
            ErrorDialog.manejaError(ex,false);
        }
    }
    private void refresca(){
        this.jTable1.setModel(new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int rowIndex,int columnIndex){
                return false;
            }
        });
        String[] columnNamesDiametro={"orden","id","Fabricante","Identificación"};
        ((DefaultTableModel)this.jTable1.getModel()).setColumnIdentifiers(columnNamesDiametro);
        this.setColumnWidth(0,0);
        this.setColumnWidth(1,0);
        try {
            for(Fabricante this_fabricante:this.getFabricante().find("orden")){
                Object[] fila={this_fabricante.getValue("orden"),this_fabricante.getValue("id"),this_fabricante,this_fabricante.getValue("numero")};
                ((DefaultTableModel)this.jTable1.getModel()).addRow(fila);
            }
            if(this.jTable1.getRowCount()==0){
                Object[] fila={"","","",""};
                ((DefaultTableModel)this.jTable1.getModel()).addRow(fila);
            }
        } catch (SQLException ex) {
            ErrorDialog.manejaError(ex,false);
        }
        this.jTable1.setUI(new DragDropRowTableUIOrdered());
        this.jTable1.setRowHeight(30);
    }
    private void doClose(int retStatus) {
        dispose();
    }   
    private void setColumnWidth(int column,int width){
        this.jTable1.getColumnModel().getColumn(column).setPreferredWidth(width);
        this.jTable1.getColumnModel().getColumn(column).setMinWidth(width);
        this.jTable1.getColumnModel().getColumn(column).setMaxWidth(width);
        this.jTable1.getColumnModel().getColumn(column).setWidth(width);
    }    

    /**
     * @return the _fabricante
     */
    public Fabricante getFabricante() {
        return _fabricante;
    }

    /**
     * @param fabricante the _fabricante to set
     */
    public void setFabricante(Fabricante fabricante) {
        this._fabricante = fabricante;
    }
    
}
